//
//  LYTabController.m
//  世界婴童网
//
//  Created by apple on 15/9/10.
//  Copyright (c) 2015年 syshop. All rights reserved.
//

#import "LYTabController.h"
#import "VVSegmentedView.h"

CGFloat const barHeight = 35;

@interface LYTabController ()<UIScrollViewDelegate,VVSegementedViewDelegate>
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;

/** 自控制器标题 */
@property (nonatomic, strong) NSArray *titles;

/** 顶部选择栏 */
@property (nonatomic, weak) VVSegmentedView *topBar;

@end

@implementation LYTabController

- (void)viewDidLoad{

    [super viewDidLoad];
    [self setUpSubView];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F5F5" alpha:1];
}

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    
    [self setupContentView];


}


- (void)setChildVCs:(NSArray *)childVCs{

    _childVCs = childVCs;
    
    [self setUpSubView];

}

- (void)disableScroll{
    
    self.contentView.scrollEnabled = NO;

}

#pragma mark - 懒加载



- (UIScrollView *)contentView{

    if (_contentView == nil) {
        
        UIScrollView *contentView = [[UIScrollView alloc] init];
        contentView.delegate = self;
        [self.view addSubview:contentView];
        _contentView = contentView;

    }
    
    return _contentView;

}

/**
 *  初始化
 */
- (void)setUpSubView{
    
    if (self.childVCs.count == 0) {
        return;
    }
    
    [self setupChildVces];
    [self setupTitlesView];
    

}

/**
 * 初始化子控制器
 */
- (void)setupChildVces
{
    NSMutableArray *titles = [NSMutableArray array];
    
    for (UIViewController *childVC in self.childVCs) {
        [self addChildViewController:childVC];
        [titles addObject:childVC.title];
    }
    
    self.titles = [titles copy];
}

/**
 * 设置顶部的标签栏
 */
- (void)setupTitlesView
{
    
    [self.topBar removeFromSuperview];
    
    VVSegmentedView *topBar = [[VVSegmentedView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, barHeight-1) titleArray:self.titles delegate:self];
    [self.view addSubview:topBar];
    self.topBar = topBar;
}

/**
 * 底部的scrollView
 */
- (void)setupContentView
{
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.contentView.frame = self.view.bounds;
    self.contentView.My_Y = barHeight;
    self.contentView.My_Height = self.view.My_Height - barHeight;
    self.contentView.pagingEnabled = YES;
    [self.view insertSubview:self.contentView atIndex:0];
    self.contentView.contentSize = CGSizeMake(self.contentView.My_Width * self.childViewControllers.count, 0);
    
    // 添加第一个控制器的view
    if (self.childVCs.count == 0) {
        return;
    }
    [self scrollViewDidEndScrollingAnimation:self.contentView];
}

/**
 * 设置导航栏
 */
//- (void)setupNav
//{
//    // 设置导航栏标题
//    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
//    // 设置背景色
//    self.view.backgroundColor = JSGlobalBg;
//}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.My_Width;
    
    // 取出子控制器
    UIViewController *vc = self.childViewControllers[index];
    vc.view.My_X = scrollView.contentOffset.x;
    vc.view.My_Y = 0; // 设置控制器view的y值为0(默认是20)
    vc.view.My_Width = scrollView.My_Width;
    vc.view.My_Height = scrollView.My_Height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.My_Width;
    [self.topBar changeVVSegementedViewWithIndex:index];
}

#pragma mark - segementViewDelegate

- (void)VVSegementedViewSelectedIndex:(NSInteger)index isClick:(BOOL)isClick{
    
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = index * self.contentView.My_Width;
    [self.contentView setContentOffset:offset animated:YES];

}


@end

